package tianqi.controller.user;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Null;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tianqi.entity.UserY;
import tianqi.entity.Zfb;
import tianqi.service.login.impl.UserServiceImpl;

@Controller
public class LoginController {
	@Autowired
	private UserServiceImpl userServiceImpl;

	// 首页展示
	@RequestMapping("/show")
	public String showIndex() {
		return "/login";
	}

	@RequestMapping("/blurLogin")
	@ResponseBody
	public String blur(String uname) {
		return userServiceImpl.blur(uname);
	}

	// 登陆校验
	@RequestMapping("/checkLogin")
	public String chackLogin(String uname, String pwd, HttpServletRequest req, HttpServletResponse res, Model m) {
		String chackLogin = userServiceImpl.chackLogin(uname, pwd, req, res, m);
		System.out.println(chackLogin);
		if ("登录成功".equals(chackLogin)) {
			return "youge";
		} else {
			m.addAttribute("msg", chackLogin);
			return "/login";
		}
	}

	// 支付登陆校验
	@RequestMapping("/loginmb")
	public String loginmb(String uname, String pwd, HttpServletRequest req, HttpServletResponse res, Model m) {
		try {
			uname = new String(uname.getBytes("iso-8859-1"), "UTF-8");

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Zfb zfb = new Zfb();
		zfb.setMname(uname);
		zfb.setMpwd(pwd);
		zfb = userServiceImpl.findmbByUserName(zfb);

		if (zfb == null) {
			m.addAttribute("msg1", "登录失败");
			m.addAttribute("msg", "2");
			return "/recharge";
		} else {
			m.addAttribute("zfb", zfb);
			return "/mbPay";
		}
	}

	@RequestMapping("/toPay")
	@ResponseBody
	public String toPay(String money, String mname, HttpServletRequest req, HttpServletResponse res, Model m) {
		Zfb zfb = new Zfb();
		// try {
		// mname=new String(mname.getBytes("iso-8859-1"),"UTF-8");
		//
		// } catch (UnsupportedEncodingException e) {
		// e.printStackTrace();
		// }
		zfb.setMonry(Double.valueOf(money));
		zfb.setMname(mname);

		if (userServiceImpl.updateByMN(zfb) > 0) {
			return "true";
		} else {
			return "false";

		}

	}

	/**
	 * 将用户修改的头像信息 更新到数据库，同时，更新session
	 * 
	 * @param usery
	 * @param req
	 * @param m
	 * @return
	 */
	@RequestMapping("/saveTX")
	@ResponseBody
	public String saveTX(UserY usery, HttpServletRequest req, Model m) {
		UserY userY = (UserY) req.getSession().getAttribute("user");
		if (userY == null) {
			return "redirect:show.tq";
		}
		if (userServiceImpl.updateTxImg(usery) > 0) {
			// 由于修改了 用户表的数据；为此做session更新
			req.getSession().setAttribute("user", userServiceImpl.findUserByUserId(userY));
			return "true";
		} else {
			return "false";
		}
	}

	/**
	 * 用户 退出登录
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping("/logOff")
	@ResponseBody
	public String saveTX(HttpServletRequest req) {
		UserY userY = (UserY) req.getSession().getAttribute("user");
		if (userY != null) {
			req.getSession().setAttribute("user", null);
			req.getSession().removeAttribute("user");
			System.out.println(req.getSession().getAttribute("user"));
			return "1";
		} else {
			return "0";
		}

	}

	@RequestMapping("/showPerson")
	public String showPerson(Model m, HttpServletRequest req) {
		UserY userY = (UserY) req.getSession().getAttribute("user");
		if (userY == null) {
			return "redirect:show.tq";
		}

		m.addAttribute("user", userServiceImpl.findUserByUserId(userY));

		return "/myPerson";
	}

	@RequestMapping("/toMyPerson")
	@ResponseBody
	public String toMyPerson(UserY user, HttpServletRequest req) {
		UserY userY = (UserY) req.getSession().getAttribute("user");
		if (userY == null) {
			return "redirect:show.tq";
		}

		if (userServiceImpl.updateMyPerson(user) > 0) {
			req.getSession().setAttribute("user", userServiceImpl.findUserByUserId(userY));
			return "1";
		} else {
			return "0";
		}

	}

}
